<!-- 处理开给渠道的销项票-->
<template>
	<el-dialog :visible.sync="dialogVisible" title="开给渠道的销项发票" width="840px" :append-to-body="false"	:close-on-click-modal="false" v-drag2anywhere>
		<el-form label-width="90px">
			<el-row>
				<el-col :span="8">
					<el-form-item label="京东订单号:">{{detail.ordercode}}</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="订单渠道:">{{detail.platform}}</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="订单总额:">{{format2Number(detail.sum_order,3)}}</el-form-item>
				</el-col>

			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item label="订单渠道:">{{detail.platform}}</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item label="折后总额:">{{format2Number(detail.sum_platform,3)}}</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">
					<el-form-item label="发票号码:">
						<el-form-item v-for="(item,index) in ticketin_ids" :key="index" label="" label-width="0"
						  prop="ticketid">
						  <div style="display: flex;">
						    <el-input v-model="ticketin_ids[index]" style="width: 250px; margin-right: 10px;" placeholder="发票号"></el-input>
						    <el-button @click="handleAddTicket" circle v-if="index==0" circle icon="el-icon-plus"></el-button>
						    <el-button @click="ticketin_ids.splice(index,1)" circle v-if="index!=0" icon="el-icon-minus"></el-button>
						  </div>
						</el-form-item>
					</el-form-item>
				</el-col>
				<el-col :span="16">
					<el-form-item label="附件上传:">
						<el-upload list-type="picture-card" ref="upload" :file-list="upconfig.imgList"
							:action="upconfig.action" :headers="upconfig.headers" :accept="upconfig.accept"
							:limit="upconfig.limit" :on-change="handleChange" :auto-upload="false"
							:on-remove="handleRemove" :on-exceed="handleLimit" multiple :on-preview="handlePreview">
							<i class="el-icon-plus"></i>
						</el-upload>
					</el-form-item>
				</el-col>
				<el-col>
					<viewer :images="imageList" ref="viewer" style="width: 0px;hegiht:0px; overflow: hidden;">
						<img v-for="(src,index) in imageList" :src="src" :key="index" style="width: 0px;" />
					</viewer>
 				</el-col>
			</el-row>
		</el-form>
		<div slot="footer" class="dialog-footer">
		  <el-button @click="dialogVisible = false" size="medium">取 消</el-button>
		  <el-button type="primary" size="medium" @click="handleSave">保存记录</el-button>
		</div>
	</el-dialog>
</template>

<script>
 	export default {
		components: {

		},
		data(){
			return{
				index:-1,
				detail:{
					ordercode:'',
					gwsid:'',
					ptamount:0,
				},
				ticketin_ids:[],
				dialogVisible:false,
				//文件上传相关变量
				upconfig: {
					//文件上传相关参数
					accept: '.jpg,.jpeg,.png,.pdf,.ofd',
					limit: 15,
					headers: {
						token: localStorage.getItem('token') != null ? localStorage.getItem('token') : ''
					},
					imgList: [],
					fileList: [],
					rids: [], //要删除的id
					action: ""
				},
				pdfVisible:false,
				imageList:[]
			}
		},
		methods:{
			init(index,data){
				this.index=index;
				this.detail=data;
				console.log(this.detail);
				if(this.detail.platformticket_ids==""){
					this.ticketin_ids=[""];
				}else{
					this.ticketin_ids=this.detail.platformticket_ids.split(",");
				}
				this.upconfig.imgList=this.detail.platformticket_attach;
				this.exchangeImgList();
			},

			exchangeImgList(){
				this.imageList=[];
				this.upconfig.imgList.forEach(item=>{
					if(item.type.indexOf("image")!=-1){
						this.imageList.push(item.url);
					}
				})
			},
			format2Number(value,len){
				var f = parseFloat(value);
				var f = Math.round(value * 100) / 100;
				var s = f.toString();
				var rs = s.indexOf('.');
				if (rs < 0) {
					rs = s.length;
					s += '.';
				}
				while (s.length <= rs + len) {
					s += '0';
				}
				return s;
			},
			handleAddTicket:function(){
				if (this.ticketin_ids[this.ticketin_ids.length-1]!="" && this.ticketin_ids.length<5) {
				    this.ticketin_ids.push('');
				}
			},
			handleSave:function(){

				 var fd=new FormData();
				 fd.append("gwsid",this.detail.gwsid);
				 fd.append("ticketin_ids",this.ticketin_ids.join(","));
				 fd.append("rmfpids",this.upconfig.rids.join(","));

				 let fid = 0;
				 this.upconfig.fileList.forEach(file => {
				 	if (file.raw) {
				 		fd.append("platformticket_" + fid, file.raw);
				 		fid++;
				 	}
				 });
				 this.$axiosUploader.post("api/admin/jd/order/update_platformticket", fd).then(res => {
					 if(res.status==200 && res.data.code==0){
						  this.detail.platformticket_status = res.data.status;
						 this.detail.platformticket_attach = res.data.attachlist;
						 this.upconfig.imgList=res.data.attachlist;
						  this.$refs.upload.clearFiles();
						  this.exchangeImgList();
						  this.$message.success({message:res.data.msg,duration:1500,showClose:true});
						  //this.upconfig.imglist = res.data.attachlist;
						  this.$emit("closeHandler",this.index,res.data.status,this.ticketin_ids,res.data.attachlist);
						  this.dialogVisible=false;
					 }else{
						  this.$message.error({message:res.data.msg,duration:1500,showClose:true});
					 }
				 });
			},
			/***********************文件上传区域开始****************/
			//文件数量超限提示
			handleLimit: function() {
				this.$message.warning("最多可上传" + this.upconfig.limit + "个文件");
			},
			handleChange: function(file, list) {
				//判断文件格式和数量
				var ext = file.name.substr(file.name.lastIndexOf('.') + 1).toLowerCase();
				var extarr = ['jpg', 'jpeg', 'png',"pdf","ofd"];
				var sizeok = file.size / 1024 / 1024 < 10;
				if (extarr.lastIndexOf(ext) == -1) {
					this.$message.error("上传文件格式必须为" + extarr.join(","));
					//删除刚上传的文件
					this.upconfig.fileList.splice(this.upconfig.fileList.length - 1, 1);
					return;
				}
				if (!sizeok) {
					this.$message.error("上传文件大小最大不超过15M");
					this.upconfig.fileList.splice(this.upconfig.fileList.length - 1, 1);
					return;
				}
				this.upconfig.fileList = list;
			},
			// 文件列表移除文件时的钩子
			handleRemove: function(file, fileList) {
				if (file.id) { //如果存储 id即为已经上传好的图片
					this.upconfig.rids.push(file.id);
				}
				this.upconfig.fileList = fileList;
			}, // 点击文件列表中已上传的文件时的钩子
			handlePreview(file) {
				this.exchangeImgList();

				if(file.type.indexOf("image")>-1){
					const viewer=this.$refs["viewer"].$viewer;
					viewer.show();
				}else if(file.type.indexOf("pdf")>-1){
					this.pdfVisible=true;
					this.$nextTick(()=>{
						this.$refs.pdfviewer.dialogVisible=true;
						this.$refs.pdfviewer.initData(file);
					})
				}
			},

		}
	}
</script>

<style scoped>

</style>
